/** @file    FormJabberGloox.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormJabberGloox.cpp 2748 2010-07-11 22:07:06Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormJabberGloox class.
 * The header for this class can be found in FormJabberGloox.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormJabberGloox.h"
 #include "ui_FormJabberGloox.h"
 #include <interfaces/devices/JabberGloox.h>

 #include <QtGui/QInputDialog>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::FormJabberGloox, "wosh::gui::IWidgetGeneric", 1.0, _static_FormJabberGloox )
 WOSH_GUI_MAP(wosh::gui::FormJabberGloox, "wosh::interfaces::devices::JabberGloox", 0.0, _static_Map_FormJabberGloox )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormJabberGloox::FormJabberGloox(QWidget *parent) : IWidgetGeneric(parent)
 {
	this->m_ui = new Ui::FormJabberGloox();
	this->m_ui->setupUi(this);

	this->setWindowTitle("JabberGloox");
	this->getWidgetURI().setName( Utilities::format("FormJabberGloox#%10lu", Utilities::randomLong()) );

	this->actionContactTitle = new QAction(this);
	this->actionContactTitle->setEnabled(false);
	this->actionContactAdd = new QAction(this);
	this->actionContactAdd->setText(tr("&Add (send Subscription).."));
	this->actionContactDelete = new QAction(this);
	this->actionContactDelete->setText(tr("&Delete (send unSubscription).."));
	this->menuContact = new QMenu(this);
	this->menuContact->addAction(this->actionContactTitle);
	this->menuContact->addSeparator();
	this->menuContact->addAction(this->actionContactDelete);
	this->menuContact->addAction(this->actionContactAdd);

	this->m_ui->tableViewContacts->getTableView()->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(this->m_ui->tableViewContacts, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	connect(this->m_ui->tableViewContacts, SIGNAL(tableContextMenuRequested(const QPoint&, const QModelIndex&)),this, SLOT(tableViewContacts_ContextMenu(const QPoint&, const QModelIndex&)) );

	connect(this->m_ui->wUpBarConfiguration, SIGNAL(updateClicked()), this, SLOT(refreshConfiguration()));

	this->propMapperCfg = new PropertiesMapper(this, true);
	connect(this->propMapperCfg, SIGNAL(send_request(const QString&, ISerializable*)), this, SLOT(executeRemoteMethod(const QString&, ISerializable*)));
	this->propMapperCfg->set(_JabberGloox_KEY_ServerAddress, this->m_ui->editorServerAddress);
	this->propMapperCfg->set(_JabberGloox_KEY_ServerPort, this->m_ui->editorServerPort);
	this->propMapperCfg->set(_JabberGloox_KEY_ServerProxy, this->m_ui->editorServerProxy);
	this->propMapperCfg->set(_JabberGloox_KEY_UserName, this->m_ui->editorUserName);
	this->propMapperCfg->set(_JabberGloox_KEY_UserPassword, this->m_ui->editorUserPassword);
	this->propMapperCfg->set(_JabberGloox_KEY_StatusMessage, this->m_ui->editorStatusMessage);
	this->propMapperCfg->set(_JabberGloox_KEY_UserAccountKey, this->m_ui->editorUserAccountKey);
	this->propMapperCfg->set(_JabberGloox_KEY_AcceptSubscription, this->m_ui->chkAcceptSubscription);
	this->propMapperCfg->set(_JabberGloox_KEY_AcceptUnSubscription, this->m_ui->chkAcceptUnSubscription);

	mapMethodButton(_JabberGloox_METHOD_ContactSynch, this->m_ui->btnContactsSynch);
	connect(this->m_ui->btnContactAdd, SIGNAL(clicked()), this, SLOT(btnContactAdd_clicked()));
	connect(this->m_ui->btnContactRemove, SIGNAL(clicked()), this, SLOT(btnContactRemove_clicked()));

 }

FormJabberGloox::~FormJabberGloox() {
	delete this->m_ui;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormJabberGloox::refreshAll() {
	this->setWindowTitle( QString("%1 [%2]").arg(this->getTargetURI().getName().c_str()).arg(this->getTargetURI().getKernelName().c_str()) );
	refreshConfiguration();
	this->m_ui->tableViewContacts->refreshData();
 }

void FormJabberGloox::refreshConfiguration() {
	List* dlProps = this->propMapperCfg->createKeysList();
	executeRemoteMethod( _METHOD_GetProperties, dlProps );
	this->m_ui->wUpBarConfiguration->sayRetrieving();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormJabberGloox::btnContactAdd_clicked() {
	bool ok = false;
	QString text = QInputDialog::getText(this, this->windowTitle(), tr("Type contact name to be added (as your.name@gmail.com)"), QLineEdit::Normal, "", &ok);
	if ( !ok || text.isEmpty())
		return;
	std::string username = string(text.toLatin1().data());
	executeRemoteMethod( _JabberGloox_METHOD_ContactAdd, new Variant(username) );
 }

void FormJabberGloox::btnContactRemove_clicked() {
	bool ok = false;
	QString text = QInputDialog::getText(this, this->windowTitle(), tr("Type contact name to be added (as your.name@gmail.com)"), QLineEdit::Normal, "", &ok);
	if ( !ok || text.isEmpty())
		return;
	std::string username = string(text.toLatin1().data());
	executeRemoteMethod( _JabberGloox_METHOD_ContactRemove, new Variant(username) );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
void FormMediaDirector::tableViewZones_doubleClicked( const QModelIndex& ) {
	Data dataUri = this->m_ui->tableViewZones->getCurrentItemAsVariantAtCol(0);
	WidgetMediaZone* mZone = new WidgetMediaZone();
	mZone->setTargetURI( dataUri.getData() );
	mZone->refreshAll();
	mZone->show();
 }
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormJabberGloox::tableViewContacts_ContextMenu( const QPoint& pos, const QModelIndex& index ) {
	QPoint globalPos = this->mapToGlobal(pos);
	if ( !index.isValid() ) return;
	Variant userAccount = this->m_ui->tableViewContacts->getTableView()->getItemAsVariant(index.row(), index.column()).toString();
	this->actionContactTitle->setText( QString("%1").arg(userAccount.toString().c_str()) );
	QAction* selected = this->menuContact->exec( globalPos );
	if ( selected == this->actionContactAdd )
		executeRemoteMethod( _JabberGloox_METHOD_ContactAdd, userAccount );
	if ( selected == this->actionContactDelete )
		executeRemoteMethod( _JabberGloox_METHOD_ContactRemove, userAccount );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormJabberGloox::busMessage( const Message& message, const Bus* ) {
	if ( message.isEmpty() || !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) ) return;
	const wosh::Response* response = message.getContent()->asResponse();

	this->m_ui->tableViewContacts->evalResponse(response);

	if ( this->propMapperCfg->evalResponse(response) )
		this->m_ui->wUpBarConfiguration->sayReady();

	promptMethodResponse(message);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
